package thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class ReentrantLock {

    public static void main(String[] args) {
        Lock lock = new java.util.concurrent.locks.ReentrantLock();

        Condition conditionNum = lock.newCondition();
        Condition conditionLet = lock.newCondition();

        char[] number = {'1','2','3','4','5','6','7','8','9'};
        char[] letter = {'A','B','C','D','E','F','G','H','I'};

        new Thread(() -> {
            lock.lock();
            try{
                for(char num : number){
                    System.out.print(num + " ");
                    conditionLet.signal();
                    conditionNum.await();
                }
                conditionLet.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally{
                lock.unlock();
            }
        }).start();

        new Thread(() -> {
            lock.lock();
            try{
                for(char let : letter){
                    System.out.print(let + " ");
                    conditionNum.signal();
                    conditionLet.await();
                }
                conditionNum.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally{
                lock.unlock();
            }
        }).start();
    }
}
